#include "dbconfig_window.h"

#define NR_IOCONFIGS_FILE "\\io setting.xml"

dbconfig_window::dbconfig_window(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    connect( ui.pushButton_1, SIGNAL(clicked()), this, SLOT(on_Save_pushButton()) );
    connect( ui.pushButton_2, SIGNAL(clicked()), this, SLOT(on_Cancel_pushButton()) );
}

dbconfig_window::~dbconfig_window()
{

}

void dbconfig_window::on_Save_pushButton()
{
    this->save_as_xml();
}

void dbconfig_window::on_Cancel_pushButton()
{
    this->close();
}

nr_error_t dbconfig_window::initialize_from_xml()
{
    QString xml_path = nr_util::get_install_dir() + QString(NR_IOCONFIGS_FILE);
    QFile file(xml_path);
    if (!file.open(QIODevice::ReadOnly))
    {
        return NR_ERROR_OPEN_FILE_FAILED;
    }
    QString error_msg;
    int error_line = 0, error_column = 0; 
    QDomDocument doc;
    if (!doc.setContent(&file, &error_msg, &error_line, &error_column))
    {
        return NR_ERROR_PARSE_XML_FAILED;
    }

    QDomElement ioconfig_element = doc.namedItem("IOSetting").toElement();

    QDomNode node = ioconfig_element.firstChild();
    while ( !node.isNull() )
    {
        QDomElement element = node.toElement();
        if (element.tagName()=="Type")
        {
            ui.lineEdit_1->setText( node.toElement().text() );
        }
        else if (element.tagName()=="Host")
        {
            ui.lineEdit_2->setText( node.toElement().text() );
        }
        else if (element.tagName()=="Port")
        {
            ui.lineEdit_3->setText( node.toElement().text() );
        }
        else if (element.tagName()=="Usernamne")
        {
            ui.lineEdit_4->setText( node.toElement().text() );
        }
        else if (element.tagName()=="Password")
        {
            ui.lineEdit_5->setText( node.toElement().text() );
        }

        node = node.nextSibling();
    }
    file.close();

    return NR_ERROR_NONE;
}

static bool create_attributte_to_element(QDomDocument &doc, QDomElement &node, const QString &domattrname, QString &domattrvalue)
{
    QDomAttr domattr_para = doc.createAttribute(domattrname);
    domattr_para.setValue( domattrvalue );
    node.setAttributeNode( domattr_para );
    return true;
}

nr_error_t dbconfig_window::save_as_xml()
{
    QString xml_path = nr_util::get_install_dir() + QString(NR_IOCONFIGS_FILE);
    QFile file(xml_path);
    if (!file.open(QIODevice::WriteOnly|QIODevice::Truncate))
    {
        return NR_ERROR_OPEN_FILE_FAILED;
    }

    QDomDocument doc;
    QDomText text;
    QDomElement element;
    QDomProcessingInstruction instruction;
    instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");
    doc.appendChild(instruction);

    QDomElement root = doc.createElement("IOSetting");
    create_attributte_to_element(doc, root, "type", ui.lineEdit_1->text() );
    create_attributte_to_element(doc, root, "host", ui.lineEdit_2->text() );
    doc.appendChild(root);

    QDomElement Type_node = doc.createElement("Type");
    QDomText Type_text = doc.createTextNode( ui.lineEdit_1->text() );
    Type_node.appendChild( Type_text );
    root.appendChild(Type_node);

    QDomElement Host_node = doc.createElement("Host");
    QDomText Host_text = doc.createTextNode( ui.lineEdit_2->text() );
    Host_node.appendChild( Host_text );
    root.appendChild(Host_node);

    QDomElement Port_node = doc.createElement("Port");
    QDomText Port_text = doc.createTextNode( ui.lineEdit_3->text() );
    Port_node.appendChild( Port_text );
    root.appendChild(Port_node);

    QDomElement Usernamne_node = doc.createElement("Usernamne");
    QDomText Usernamne_text = doc.createTextNode( ui.lineEdit_4->text() );
    Usernamne_node.appendChild( Usernamne_text );
    root.appendChild(Usernamne_node);

    QDomElement Password_node = doc.createElement("Password");
    QDomText Password_text = doc.createTextNode( ui.lineEdit_5->text() );
    Password_node.appendChild( Password_text );
    root.appendChild(Password_node);
    
    QTextStream out(&file);
    doc.save(out,4);
    file.close();

    QMessageBox msgBox;
    msgBox.setText("Database Configuration has saved as \"io setting.xml\".");
    msgBox.exec();

    return NR_ERROR_NONE;
}
